﻿using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {

	public Node[,] nodes;
	public int Rows;
	public int Columns;
	public Transform spawn;
	public Vector2 min;
	public Vector2 max;
	private Vector2 count;

	public Vector2 position;

	void Start()
	{
		Columns = Mathf.Abs((int)((max.x-min.x)/spawn.localScale.x));
		Rows = Mathf.Abs((int)((max.y-min.y)/spawn.localScale.y));


		GameObject path = new GameObject();
		path.name = "Pathfinding";

		nodes = new Node[Columns,Rows];

		for (int x = 0; x < Columns; x++) 
		{
			for (int y = 0; y < Rows; y++) 
			{
				Vector3 spawnPoint = Vector3.zero;

				spawnPoint.x = min.x+(x*spawn.localScale.x)+spawn.localScale.x/2;
				spawnPoint.y = min.y-(y*spawn.localScale.y)-spawn.localScale.y/2;
				spawnPoint.z = spawn.position.z;

				Transform t = Instantiate(spawn,spawnPoint,spawn.rotation) as Transform;
				t.name = x+"/"+y;
				t.gameObject.AddComponent("Node");
				t.parent = path.transform;
				nodes[x,y] = t.GetComponent<Node>();
			}
		}

		path.transform.position = new Vector3(position.x,position.y,0);
	}
}
				